package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.anno.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishCategoryVO;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private FlavorMapper flavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 新增菜品和对应的口味
     * @param dishDTO
     */
    @Override

    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        //新增菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish); //属性拷贝
        dishMapper.insert(dish);

        //获取到菜品的id
        Long dishId = dish.getId();
        //新增菜品对应的口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!(flavors.isEmpty())){
            //设置每个菜品id
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
            //批量插入
            flavorMapper.insertBrath(flavors);

        }


    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult<DishVO> pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //开始分页查询
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult<DishVO>(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBarch(List<Long> ids) {
        //判断当前菜品是否可以删除--是否在起售中
        for (Long id : ids) {
             Dish dish =  dishMapper.getById(id);
             if (dish.getStatus() == StatusConstant.ENABLE){
                 throw  new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
             }
        }
        
        //判断当前菜品是否可以删除--是否与其他套餐所关联
        List<SetmealDish>  setmealDishList =  setmealDishMapper.getDishIdById(ids);
        if (!(setmealDishList.isEmpty())){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
//        //删除菜品的同时也要删除对应的口味
//        for (Long id : ids) {
//            dishMapper.deleteById(id);
//            flavorMapper.deleteByDishId(id);
//        }
//        优化
        //删除菜品的同时也要删除对应的口味
        //删除菜品
        dishMapper.deleteByIds(ids);
        flavorMapper.deleteByDishIds(ids);

    }

    @Override
    @Transactional
    public DishVO getDishById(Long id) {
        //查询菜品的信息
        DishVO dishVO = new DishVO();
        Dish dish = dishMapper.getById(id);
        BeanUtils.copyProperties(dish,dishVO); //属性拷贝
        //根据菜品id 查询对应的口味 并且封装到 dishVo中
        List<DishFlavor> dishFlavors =  flavorMapper.getByDishId(id);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }
    /**
     * 修改菜品信息
     * @param dishDTO
     */

    @Override
    @Transactional
    public void updateDish(DishDTO dishDTO) {
            Dish dish = new Dish();
            BeanUtils.copyProperties(dishDTO,dish);
            //修改菜品
            dishMapper.updateDish(dish);
            //获取到需要修改菜品的id
            Long dishID = dishDTO.getId();
            //通过菜品id修改菜品对应的口味 思路 ： 可以将原来的口味数据 删掉
        //然后 重新插入 传过来的口味数据就好了
        //删除 之前原有的口味
            flavorMapper.deleteByDishId(dishID);
            //重新插入 传过来的口味数据
            //插入的时候 dish_id是没有值的 需要遍历赋值
            List<DishFlavor> dishFlavors = dishDTO.getFlavors();
            dishFlavors.forEach(dishFlavor -> dishFlavor.setDishId(dishID));
            flavorMapper.insertBrath(dishFlavors);


    }

    /**
     *
     *修改 起售停售状态信息
     *
     * @param status
     */
    @Override
    public void updateStatus(Integer status,Long id) {
            Dish dish = new Dish();
            dish.setStatus(status);
            dish.setId(id);
            dishMapper.updateDish(dish);

    }

    @Override
    public List<DishCategoryVO> getByCategoryId(Long categoryId) {
        List<DishCategoryVO> dishCategoryVOList = dishMapper.getByCategoryId(categoryId);
       return dishCategoryVOList;
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        //通过 分类id查询 菜品列表
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = flavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
